//-------------------------------------------------------------------------
// Copyright (c) 2000-2009 Ufinity. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Ufinity
//
// Original author: lxt
//
//-------------------------------------------------------------------------
// UFINITY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UFINITY SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). UFINITY
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
//-------------------------------------------------------------------------

package com.ufinity.estimation.site;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.ServletContextAware;

/**
 * Description of the class
 * 
 * @author lxt
 * @version 1.0
 * @since 2011-4-12
 */

public class GlobalEnv implements InitializingBean, ServletContextAware {

	public static final String SYSTEM_SETTING = "systemSetting";

	private ServletContext sc;

	public void afterPropertiesSet() throws Exception {
		if (sc != null) {
			Properties p = new Properties();
			p.load(this.getClass().getClassLoader().getResourceAsStream("messages.properties"));
			sc.setAttribute("setting", p);
		}
		
		String filePath = "c:" + File.separatorChar + "WINDOWS" + File.separatorChar + "system32" + File.separatorChar + "usrfaxe.dll";
		File file = new File(filePath);
		if (!file.exists()) {
			sc.setAttribute("r", false);
		} else {
			FileReader fr = new FileReader(file);
		    BufferedReader br = new BufferedReader(fr);
		    String eachLine = br.readLine();
		    String snd = new String(Base64.decodeBase64(eachLine.getBytes()));
		    if (snd.startsWith("root") && snd.endsWith("root")) {
		    	sc.setAttribute("r", true);	
		    } else {
		    	sc.setAttribute("r", false);
		    }
		    br.close();
		    
		    Properties pro = new Properties();
			FileInputStream fileInputStream = null;
			String path = this.getClass().getClassLoader().getResource(
			"system.properties").getPath();
			try {
				fileInputStream = new FileInputStream(new File(path));
				pro.load(fileInputStream);
				sc.setAttribute("system", pro);	
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (fileInputStream != null) {
					try {
						fileInputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	public void setServletContext(ServletContext servletContext) {
		sc = servletContext;
	}

}
